# -*- coding: utf-8 -*-
'''
Copyright(C) 2013 "Kevin Xu <ssonxu@gmail.com>"

This file is part of acqu-center.

acqu-center is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

acqu-center is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with acqu-center.  If not, see <http://www.gnu.org/licenses/>.
'''

from google.appengine.api import urlfetch
import re
import logging
from Predef import Predef as PRE

class parser:

    @staticmethod
    def readContext(url):
        try:
            urlfetch.set_default_fetch_deadline(45)
            result = urlfetch.fetch(url)
            if result.status_code == 200:
                return result.content
            else:
                return None
        except urlfetch.InvalidURLError, e:
            logging.error(e)
            return None
        except urlfetch.DownloadError, e:
            logging.error(e)
            return None
        except urlfetch.ResponseTooLargeError, e:
            logging.error(e)
        except urlfetch.Error, e:
            logging.error(e)
            return None

    @staticmethod
    def getMatch(match):
        if match is None:
            return None
        r=""
        for m in match.groups():
            r+=m
        return r

    @staticmethod
    def printMatch(match):
        if match is None:
            print 'None'
        else:
            #print u'<Match: %s, groups=%s>'.encode('utf-8') % (match.group(), match.groups())
            print u'<Match: {}, groups={}>'.encode('utf-8').format(match.group(), match.groups())

    @staticmethod
    def getCharset(s):
        char_re = re.compile(r"(?is)content=[\"'].*?;\s*charset=(.*?)[\"']")
        c = char_re.search(s)
        if c is not None:
            return c.group(1)
        return None

    @staticmethod
    def testRE( p , url ):
        c = parser.readContext(url)
        charset=parser.getCharset(c)
        print 'charset:%s' % charset
        if charset in PRE.charsets:
            c = c.decode(charset)
            c = c.encode('utf-8')

        for l in p:
            print 'searching:<%r>' % l
            parser.printMatch(re.search( l , c ))

    def __init__(self, box):
        self.box=box

    def run(self):
        if self.box is None:
            raise TypeError,'Empty box'

        context = parser.readContext(self.box.url)
        if context is None or context == '':
            logging.error("urlfetch failed, no date from <%s>", self.box.url)
            return

        # convert to utf-8
        charset=parser.getCharset(context)
        if charset in PRE.charsets:
            context = context.decode(charset)
            context = context.encode('utf-8')

        for k in self.box.stuffs:
            b=re.compile(k.pattern)
            match=parser.getMatch(b.search(context))
            if match is None:
                logging.error("Bad pattern\"%s\" for url:<%s>", k.pattern, self.box.url)
            else:
                #logging.info("Got it <%s>", match)
                k.setVal(PRE.UTF8(match))

